<template>
  <div>
    <el-upload
      class="upload-demo"
      action=""
      :before-upload="beforeUpload"
      ref="newupload"
      :show-file-list="false"
      :auto-upload="false"
      :on-change="uploadFile"
      :on-success="handleSuccess">
      <!--<el-button type="primary" @click="addPlcPointData" style="margin: 5px;float: right">新增</el-button>-->
      <el-button slot="trigger"  type="primary">导入</el-button>
    </el-upload>
  </div>
</template>

<script>
  import {newVideo} from "../../utils/public";
  export default {
    name: "UploadPlcPoint",
    data() {
      return {
        device_id:0
      }
    },
    methods:{
      //做校验
      beforeUpload (file) {
        let me=this;
        let fd = new FormData()
        fd.append('upload_file', file)
        fd.append('action_type', 'importPlcPointFromExcel')
        fd.append('device_id', this.device_id)
        newVideo(fd).then(res => {
          /*file.onSuccess(res.payload.file[0])   // 接口返回成功时处理，调用onSuccess*/


          if(res.data.success==true){
            const h = this.$createElement;
            this.$message({
              message: h('p', null, [
                h('i', {style: 'color: green'}, '通过: '),
                h('i', {style: 'color: blue'}, `提交成功`)
              ])
            });
            me.$parent.queryData();
          }else{
            const h = this.$createElement;
            this.$message({
              message: h('p', null, [
                h('i', {style: 'color: green'}, '失败: '),
                h('i', {style: 'color: blue'},res.data.message)
              ])
            });
          }

        })
        return true
      },
      handleSuccess(response, file, fileLis){
        let me=this;
        alert(me.device_id);
        alert(file.name+'上传成功');
        console.log('上传成功');
        //刷新页面
        me.$parent.queryData
        alert('页面刷新成功');
        /*    me.$parent.queryData();
            alert('页面刷新成功')*/

      },
      uploadFile(){
        this.$refs.newupload.submit()
      },
      handleError(err, file, fileList){
        alert(err);
        console.log('上传失败')
      }
    },
    created() {
      let me = this;
      me.device_id=me.$parent.$route.params.device_id;
    }


  }
</script>

<style lang="scss" scoped>

</style>
